package com.tutorialspoint.annotation;

import com.tutorialspoint.annotation.CustomEventPublisher;
import com.tutorialspoint.base.MyBase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp extends MyBase {
	public static void main(String[] args) {
		ApplicationContext context_required = 
				new ClassPathXmlApplicationContext("Beans.xml");
		// context started event
		((ConfigurableApplicationContext)context_required).start();
		
		Student student = (Student) context_required.getBean("student");
		System.out.println("Name : " + student.getName());
		System.out.println("Age : " + student.getAge());
		System.out.println("Sex : " + student.getSex());
		System.out.println(student.sayHello());
		
		// get custom event publisher
		CustomEventPublisher cvp = 
				(CustomEventPublisher) context_required.getBean("customEventPublisher");
		
		// context custom event
		cvp.publish();
		
		// context stopped event
		((ConfigurableApplicationContext)context_required).stop();
		
		// close context
		MainApp.closeContext(context_required);
		
		// configuration
		ApplicationContext ctx = 
				new AnnotationConfigApplicationContext(HelloWorldConfig.class);
		HelloWorld hw = ctx.getBean(HelloWorld.class);
		System.out.println(hw.getMessage());
		((AnnotationConfigApplicationContext)ctx).close();
		
	}
}
